package cn.tedu.web.fqmz.servlet;

import cn.tedu.web.fqmz.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = "/user/insert")
public class UserInsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String sql = "";
        if ("null".equals(id)) {
            sql = "INSERT INTO `user` (`username`,`password`,name) VALUES (?,MD5('123'),?)";
            JdbcUtils.modify(sql, username, name);
        } else {
            sql = "UPDATE `user` SET username = ?, `name` = ? WHERE id = ?";
            JdbcUtils.modify(sql, username, name,id);
        }

        resp.sendRedirect("/fqmz/user-list.jsp");
    }
}
